home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 43
/
Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso
/
-serious-
/
misc
/
gethelp
/
source
/
get_title.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-06-14
|
6KB
|
302 lines
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <exec/memory.h>
#include <exec/types.h>
#define MAXLEN 256
#define MEMTYPE MEMF_PUBLIC | MEMF_CLEAR
static char strbuf[MAXLEN]; /* string buffer */
extern int debug; /* debug flag */
/*
* int *findstring(lineptr, nlines, findstr, flag_whole, flag_case)
*
* return TRUE if findstr found in array of lines linptr
* flag_whole: look for whole word
* flag_case: look for matching case
*
*/
int findstring(lineptr, nlines, findstr, flag_whole, flag_case)
char *lineptr[];
int nlines;
char *findstr;
int flag_whole;
int flag_case;
{
extern int strindex(), isword();
int flag_found = 0, pos = 0;
while (--nlines >= 0) {
if ((pos = strindex(*lineptr, findstr, flag_case)) > -1) {
if (debug) printf ("Found it at pos=%d!\n",pos);
flag_found = 1;
if (flag_whole)
if (!(isword(*lineptr, findstr, pos))) flag_found = 0;
}
*lineptr++;
}
return(flag_found);
}
int findtitle(filename, lineptr, nlines, fp)
char *filename,
*lineptr[];
int nlines;
FILE *fp;
{
extern int strindex(), strcmptext();
extern char *midstr(), *solidstr();
extern void add_url();
int pos = 0, startpos = 0, flag_found = 0;
char titlestr[MAXLEN];
while ((--nlines > 0) && (*lineptr)) {
if (debug) printf("found flag = %d\n",flag_found);
/* check for <TITLE> tag */
if ((pos = strindex(*lineptr, "<TITLE>", 0)) > -1) {
if (debug) printf("Got <TITLE>!\n");
flag_found = 1;
startpos = pos + 7;
/* anything else on this line? */
if (startpos != strlen(*lineptr)) {
/* yes there is, get it */
if (debug) printf("remainder was %s\n",midstr(*lineptr, startpos, strlen(*lineptr)));
/* check for </TITLE> tag */
if ((pos = strindex(*lineptr, "</TITLE>", 0)) > -1) {
nlines = 0;
} else pos = strlen(*lineptr);
if (debug) printf("reading title..%s\n",midstr(*lineptr,startpos,pos));
strcpy(titlestr,(char *)midstr(*lineptr,startpos,pos));
} /* else nothing */
}
/* proceed through subsequent line(s) */
*lineptr++;
startpos = 0;
if (flag_found && nlines) {
/* check for </TITLE> tag */
if ((pos = strindex(*lineptr, "</TITLE>", 0)) > -1) {
nlines=0;
startpos = pos + 8;
/* anthing preceeding the tag */
if (strcmptext("</TITLE>",*lineptr))
strcpy(titlestr,(char *)midstr(*lineptr,startpos,pos));
else nlines = 0;
} else strcpy(titlestr,*lineptr);
}
}
if (flag_found) {
if (debug) printf("Title is '%s'\n",(char *)solidstr(titlestr));
/* add title to report file */
add_url(fp, filename, solidstr(titlestr));
}
return(flag_found);
}
/*
* int strcmptext(s,t)
*
* return <0 if s<t, 0 if s==t, >0 if s>t
* ignoring whitespace in t
*
*/
int strcmptext(s, t)
char *s, *t;
{
int i = 0, j = 0;
while(s[i] != '\0')
if (!(s[i] == t[j])) {
if ((t[j] == ' ')||(t[j] == '\t')) j++; else return(s[i]-t[j]);
} else {
i++; j++;
}
return(0);
}
/*
* char *midstr(s,startpos,endpos)
*
* return a substring of s from position startpos to endpos
* use a string buffer to avoid hacking up arguments!
*
*/
char *midstr(s, startp, endp)
char *s;
int startp, endp;
{
extern char strbuf[MAXLEN];
int i=0, pos=0;
pos = startp;
while ((strbuf[i++] = s[pos++]) != '\0' && pos<endp);
strbuf[i] = '\0';
return(strbuf);
}
/*
* char *solidstr(s)
*
* return a substring of s without leading or trailing whitespace
* use a string buffer to avoid hacking up arguements!
*
*/
char *solidstr(s)
char *s;
{
extern char strbuf[MAXLEN];
int i=0, j=0;
/* skip leading whitespace */
while (isspace(s[j])) j++;
/* copy remainder of string */
while ((strbuf[i] = s[j++]) != '\0') i++;
/* backtrack to confirm end of string */
while (isspace(strbuf[i])) strbuf[i--] = '\0';
return(strbuf);
}
/*
* int strindex(s, t, flag)
*
* return position of string s in t or -1 if not found
* if flag is TRUE match case
*
*/
int strindex(s, t, flag)
char *s, *t;
int flag;
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++) {
if (flag)
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++);
else
for (j=i, k=0; t[k]!='\0' && tolower(s[j])==tolower(t[k]); j++, k++);
if (t[k] == '\0') return(i);
}
return(-1);
}
/*
* int isword(s,t)
*
* returns TRUE if string t was a complete word in s at position pos
*/
int isword(s, t, pos)
char *s, *t;
int pos;
{
if (pos > 0)
if (isalnum(s[pos-1])) return(0);
if (isalnum(s[pos + strlen(t)])) return(0);
return(1);
}
/*
* int readlines(lineptr, maxlines, fp)
*
* read file fp with maximum lines maxlines into array lineptr
* return number of lines
*/
int readlines(lineptr, maxlines, fp)
char *lineptr[];
int maxlines;
FILE *fp;
{
extern struct Remember *RememberLinesPtr; /* Lines memory area */
int len, /* length of current line */
nlines; /* number of lines found */
char line[MAXLEN]; /* storage for each line */
UBYTE *AllocRemember();
nlines=0; /* reset lines counter */
while((fgets(line,MAXLEN,fp)) != NULL)
if (nlines >= maxlines)
return(-1);
else {
len = strlen(line);
if (lineptr[nlines] = AllocRemember(&RememberLinesPtr, (ULONG)len, MEMTYPE)) {
line[len-1] = '\0';
strcpy(lineptr[nlines++],line);
} else {
printf("Out of Memory in readlines!\n");
return(-1);
}
}
return(nlines);
}
/*
* void writelines(lineptr, nlines)
*
* print nlines lines of array lineptr
*/
void writelines(lineptr, nlines)
char *lineptr[];
int nlines;
{
while (--nlines >= 0)
printf("%s\n", *lineptr++);
}